//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdmHestonHullWhiteEquityPart.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Experimental/Finitedifferences/FdmMesher.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Experimental/Finitedifferences/TripleBandLinearOp.h>
using namespace Cephei::QL::Termstructures;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart::CFdmHestonHullWhiteEquityPart (Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher, Cephei::QL::Termstructures::IYieldTermStructure^ qTS) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CFdmMesher^ _Cmesher;
    CYieldTermStructure^ _CqTS;
    try
    {
#ifdef HANDLE
        _phFdmHestonHullWhiteEquityPart = NULL;
#endif
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        _CqTS = safe_cast<CYieldTermStructure^> (qTS);
        _CqTS->Lock();
        boost::shared_ptr<QuantLib::YieldTermStructure>& _qTS = static_cast<boost::shared_ptr<QuantLib::YieldTermStructure>&> (_CqTS->GetShared ()); 
        _ppFdmHestonHullWhiteEquityPart = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteEquityPart> (new QuantLib::FdmHestonHullWhiteEquityPart ( _mesher,  _qTS ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmesher != nullptr) _Cmesher->Unlock();
        if (_CqTS != nullptr) _CqTS->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart::CFdmHestonHullWhiteEquityPart (boost::shared_ptr<QuantLib::FdmHestonHullWhiteEquityPart>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonHullWhiteEquityPart = NULL;
#endif
	_ppFdmHestonHullWhiteEquityPart = &childNative;
    
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart::CFdmHestonHullWhiteEquityPart (QuantLib::FdmHestonHullWhiteEquityPart& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmHestonHullWhiteEquityPart = NULL;
#endif
	_ppFdmHestonHullWhiteEquityPart = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteEquityPart> (&childNative);
    
    _FdmHestonHullWhiteEquityPartOwner = owner;
    
}

Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart::CFdmHestonHullWhiteEquityPart (CFdmHestonHullWhiteEquityPart^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonHullWhiteEquityPart = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmHestonHullWhiteEquityPart = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteEquityPart> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart::CFdmHestonHullWhiteEquityPart (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonHullWhiteEquityPart = NULL;
#endif
	if (!t->IsSubclassOf(CFdmHestonHullWhiteEquityPart::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart::CFdmHestonHullWhiteEquityPart (QuantLib::Handle<QuantLib::FdmHestonHullWhiteEquityPart>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmHestonHullWhiteEquityPart = &childNative;
	_ppFdmHestonHullWhiteEquityPart = &static_cast<boost::shared_ptr<QuantLib::FdmHestonHullWhiteEquityPart>>(childNative.currentLink());
    
    _FdmHestonHullWhiteEquityPartOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart::CFdmHestonHullWhiteEquityPart (QuantLib::Handle<QuantLib::FdmHestonHullWhiteEquityPart> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmHestonHullWhiteEquityPart = &childNative;
	_ppFdmHestonHullWhiteEquityPart = &static_cast<boost::shared_ptr<QuantLib::FdmHestonHullWhiteEquityPart>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart::CFdmHestonHullWhiteEquityPart (QuantLib::FdmHestonHullWhiteEquityPart childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonHullWhiteEquityPart = NULL;
#endif
	_ppFdmHestonHullWhiteEquityPart = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteEquityPart> (new QuantLib::FdmHestonHullWhiteEquityPart (childNative));
    
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart::~CFdmHestonHullWhiteEquityPart ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmHestonHullWhiteEquityPart != NULL)
    {
	    delete _ppFdmHestonHullWhiteEquityPart;
        _ppFdmHestonHullWhiteEquityPart = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart::!CFdmHestonHullWhiteEquityPart ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmHestonHullWhiteEquityPart != NULL)
    {
	    delete _ppFdmHestonHullWhiteEquityPart;
    }
}
QuantLib::FdmHestonHullWhiteEquityPart& Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart::GetReference ()
{
    if (_ppFdmHestonHullWhiteEquityPart == NULL) throw gcnew NativeNullException ();
	return **_ppFdmHestonHullWhiteEquityPart;
}
boost::shared_ptr<QuantLib::FdmHestonHullWhiteEquityPart>& Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart::GetShared ()
{
    if (_ppFdmHestonHullWhiteEquityPart == NULL) throw gcnew NativeNullException ();
	return *_ppFdmHestonHullWhiteEquityPart;
}
QuantLib::FdmHestonHullWhiteEquityPart* Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart::GetPointer ()
{
    if (_ppFdmHestonHullWhiteEquityPart == NULL) throw gcnew NativeNullException ();
	return &**_ppFdmHestonHullWhiteEquityPart;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmHestonHullWhiteEquityPart>& Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart::GetHandle ()
{
	if (_phFdmHestonHullWhiteEquityPart == NULL)
	{
		_phFdmHestonHullWhiteEquityPart = new Handle<QuantLib::FdmHestonHullWhiteEquityPart> (*_ppFdmHestonHullWhiteEquityPart);
	}
	return *_phFdmHestonHullWhiteEquityPart;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart::HasNative () 
{
	return (_ppFdmHestonHullWhiteEquityPart != NULL);
}

Cephei::QL::Experimental::Finitedifferences::ITripleBandLinearOp^ Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart::GetMap::get ()
{
    try
    {
    	QuantLib::TripleBandLinearOp& _rv = (QuantLib::TripleBandLinearOp&)(*_ppFdmHestonHullWhiteEquityPart)->getMap ( );   
        Cephei::QL::Experimental::Finitedifferences::CTripleBandLinearOp^ _nrv = gcnew Cephei::QL::Experimental::Finitedifferences::CTripleBandLinearOp (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Finitedifferences::IFdmHestonHullWhiteEquityPart^ Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart::SetTime (Double t1, Double t2)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Time _t1 = (QuantLib::Time)ValueHelper::Convert (t1);
        QuantLib::Time _t2 = (QuantLib::Time)ValueHelper::Convert (t2);
    	(*_ppFdmHestonHullWhiteEquityPart)->setTime ( _t1,  _t2 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFdmHestonHullWhiteEquityPart^ Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteEquityPart_Factory::Create (Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher, Cephei::QL::Termstructures::IYieldTermStructure^ qTS)
{
    return gcnew CFdmHestonHullWhiteEquityPart ( mesher,  qTS);
}
